package com.example.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.mapper.RoleMenuMapper;
import com.example.pojo.Role;
import com.example.mapper.RoleMapper;
import com.example.pojo.RoleMenu;
import com.example.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.util.JwtTokenUtil;
import com.example.vo.ResultVo;
import com.example.vo.respvo.Option;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.omg.CORBA.OBJ_ADAPTER;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import javax.xml.transform.Result;
import java.sql.Date;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author wsl
 * @since 2022-03-27
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Autowired
    private RoleMenuMapper roleMenuMapper;

    @Override
    @Transactional
    public ResultVo disableBatchRole(Integer[] rIds) {
        int i = baseMapper.disableBatchRole(rIds);
        if(i > 0){
            return ResultVo.success("禁用成功。");
        }
        return ResultVo.error("禁用失败");
    }

    @Override
    public PageInfo<Role> getRolePageInfo(Integer pageNum,Integer pageSize,String roleName, String roleNameCh, Integer enabled, Date start, Date end) {
        PageHelper.startPage(pageNum,pageSize);
        List<Role> roles = baseMapper.selectRolePageInfo(roleName, roleNameCh, enabled, start, end);
        roles.forEach(r->{
            List<Integer> list = baseMapper.selectRoleMenuIds(r.getId());
            r.setMenuIds(list.toArray(new Integer[0]));
        });
        return new PageInfo<>(roles);
    }

    @Override
    @Transactional
    public ResultVo insertRole(Role role) {
        int i = baseMapper.insert(role);
        return i > 0 ? ResultVo.ok() : ResultVo.error();
    }

    @Override
    @Transactional
    public ResultVo updateRole(Role role) {
        if(role.getId() == 1){
            ResultVo.error("系统角色，不可以修改!");
        }
        //1.修改角色基本信息
        int i = baseMapper.updateById(role);
        return i > 0 ? ResultVo.ok() : ResultVo.error();
    }

    @Override
    public ResultVo getAssignRoleList(Integer empId) {
        List<Map<String, Object>> roleList = baseMapper.selectMaps(new QueryWrapper<Role>().select("id as value,role_name_ch as label,enabled"));
        List<Integer> assignedRoles = baseMapper.selectUserAssignedRoles(empId.toString());
        Map<String, Object> map = new HashMap<>();
        map.put("roleList",roleList);
        map.put("assignedRoles",assignedRoles);
        return ResultVo.response(map);
    }

    @Override
    public Boolean getHasSomeRoleName(Integer id, String roleNameCh) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.notIn(id != null,"id",id)
                .eq("role_name_ch",roleNameCh);
        Long aLong = baseMapper.selectCount(wrapper);
        return aLong > 0;
    }

    @Override
    public Boolean getHasSomeRoleCode(Integer id, String roleName) {
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.notIn(id != null,"id",id)
                .eq("role_name",roleName);
        Long aLong = baseMapper.selectCount(wrapper);
        return aLong > 0;
    }
}
